package com.gz.mylife.app.keys;
/**
 * 注册门禁账号
 */
import java.lang.ref.WeakReference;

import com.google.gson.Gson;
import com.gz.mylife.App;
import com.gz.mylife.R;
import com.gz.mylife.app.BaseActivity;
import com.gz.mylife.http.AsyncHttpClient;
import com.gz.mylife.http.AsyncHttpResponseHandler;
import com.gz.mylife.http.RequestParams;
import com.zl.smartmall.library.po.LoginData;
import com.zl.smartmall.library.util.Constants;

import android.view.View.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class RegisterActivity extends BaseActivity implements OnClickListener {


	private String phone;
	private String register_password;
	private String register_password2;
	private String password;
	private String phone_sn;
	private EditText etNewPhone;
	private EditText etNewPwd;
	private EditText etNewPwd2;
	private EditText etSms;
	private int time = 59;
	private MyHandler mHandler;
	private TextView tv_resend;//获取验证码的按钮文字
	private Button btn_update;//确认提交按钮
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_find);
		setActionbarTitle("注册门禁账号", false);
		etNewPhone = (EditText) findViewById(R.id.et_new_num);//注册手机号
		etNewPwd = (EditText) findViewById(R.id.et_find_pwd);
		etNewPwd2 = (EditText) findViewById(R.id.et_find_pwd2);
		tv_resend = (TextView) findViewById(R.id.tv_resend);
		tv_resend.setOnClickListener(this);
		etSms = (EditText) findViewById(R.id.et_sms);//验证码
		btn_update = (Button) findViewById(R.id.btn_update);
		btn_update.setText("注册账号");
		btn_update.setOnClickListener(this);
		TelephonyManager TelephonyMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE); 
		phone_sn = TelephonyMgr.getDeviceId();
		mHandler = new MyHandler(this);
	}
	private Runnable leftTime = new Runnable() {

		@Override
		public void run() {
			if(time>0){
				tv_resend.setEnabled(false);
				tv_resend.setTextColor(getResources().getColor(R.color.divid_item_gray));
				tv_resend.setText("("+time+")后重新获取");
				mHandler.postDelayed(leftTime, 1000);
				time--;
			}else{
				time = 59;
				tv_resend.setEnabled(true);
				tv_resend.setTextColor(getResources().getColor(R.color.global_orange));
				tv_resend.setText("重新获取");
				mHandler.removeCallbacks(leftTime);
			}

		}
	};
	private static class MyHandler extends Handler {
		private RegisterActivity mOuter;

		public MyHandler(RegisterActivity outer) {
			this.mOuter = outer;
		}

		public void release() {
			new WeakReference<RegisterActivity>(mOuter);
		}

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				break;

			default:
				break;
			}
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_update:
			phone = etNewPhone.getText().toString().trim();
			register_password = etNewPwd.getText().toString().trim();
			register_password2 = etNewPwd2.getText().toString().trim();
			password = etSms.getText().toString().trim();

			if (register_password.equals(register_password2)) {

			}else{
				Toast.makeText(this, "两次密码输入不一致！", 0).show();
				return;
			}
			if (TextUtils.isEmpty(phone) || TextUtils.isEmpty(register_password)|| TextUtils.isEmpty(register_password2)|| TextUtils.isEmpty(password)) {

				if (TextUtils.isEmpty(phone)) {

					Toast.makeText(this, "请输入手机号！！！", 0).show();
					return;
				}
				if (TextUtils.isEmpty(register_password)) {

					Toast.makeText(this, "请输入密码！！！", 0).show();
					return;
				}
				if (TextUtils.isEmpty(register_password2)) {

					Toast.makeText(this, "请在再次输入密码！！！", 0).show();
					return;
				}
				if (TextUtils.isEmpty(password)) {

					Toast.makeText(this, "请输入验证码！！！", 0).show();
					return;
				}
				return;

			}
			String url_register = Constants.REGISTER_URL;
			AsyncHttpClient httpClient_register=new AsyncHttpClient();
			RequestParams params_register=new RequestParams();
			params_register.put("phone", phone);
			params_register.put("register_password", register_password);
			params_register.put("password", password);
			params_register.put("phone_sn", phone_sn);
			//这个是get方法请求，自己调配，2者写一就行
			httpClient_register.get(url_register ,params_register,new AsyncHttpResponseHandler(){

				@Override
				public void onSuccess(String content) {
					super.onSuccess(content);
					Gson gson = new Gson();
					LoginData mLoginData = gson.fromJson(content, LoginData.class);
					//验证成功
					if(mLoginData.ref==0){
						Intent i = new Intent(RegisterActivity.this,LoginDoorActivity.class);
						RegisterActivity.this.startActivity(i);
						finish();
					}else {
						Toast.makeText(RegisterActivity.this,mLoginData.msg, 0).show();
					}
				}
				@Override
				public void onFailure(Throwable error, String content) {
					super.onFailure(error, content);

					Toast.makeText(RegisterActivity.this,"请求失败...", 0).show();
				}
			});
			break;
		case R.id.tv_resend:
			phone = etNewPhone.getText().toString().trim();

			if (TextUtils.isEmpty(phone)) {
				App.mTimeState = false;
				Toast.makeText(this, "请输入手机号！！！", 0).show();
				return;
			}else{
				tv_resend.setText("("+time+")后重新获取");
				mHandler.postDelayed(leftTime, 1000);
			}
			App.mTimeState = true;
			String url = Constants.SMS_URL;
			AsyncHttpClient httpClient=new AsyncHttpClient();
			RequestParams params=new RequestParams();
			params.put("phone", phone);
			//这个是get方法请求，自己调配，2者写一就行
			httpClient.get(url ,params,new AsyncHttpResponseHandler(){

				@Override
				public void onSuccess(String content) {
					super.onSuccess(content);
					Gson gson = new Gson();
					LoginData mLoginData = gson.fromJson(content, LoginData.class);
					//验证成功
					if(mLoginData.ref==0){
					}else {
						Toast.makeText(RegisterActivity.this,mLoginData.msg, 0).show();
					}
				}

				@Override
				public void onFailure(Throwable error, String content) {
					super.onFailure(error, content);

					Toast.makeText(RegisterActivity.this,"发送失败，请检查您的网络。", 0).show();
				}
			});
			break;
		}
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mHandler.removeCallbacks(leftTime);
		mHandler.release();
	}

}
